﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;

namespace CNav.Menu
{
    public class MenuManager
    {
        public List<ISiteLink> GetSiteMenuItems(RequestContext context)
        {
            var url = new UrlHelper(context);

            var items = new List<ISiteLink>();
            // Top Level
            items.Add(new SiteMenuItem { Id = 1, ParentId = 0, Text = "New", Url =url.Action("NewEnquiry", "Enquiry"), OpenInNewWindow = false, SortOrder = 1 });
            // Services Children
            items.Add(new SiteMenuItem { Id = 2, ParentId = 1, Text = "New Enquiry", Url = url.Action("NewEnquiry", "Enquiry"), OpenInNewWindow = false, SortOrder = 0 });
        
            SelectItem(context, items);

            return items;
        }

        public List<ISiteLink> GetBusinesSiteLinks(RequestContext context)
        {
            var url = new UrlHelper(context);
            var items = new List<ISiteLink>();
            // Top Level
            items.Add(new SiteMenuItem { Id = 1, ParentId = 0, Text = "List All User", Url = url.Action("Index", "UserProfile"), OpenInNewWindow = false, SortOrder = 1 });
            items.Add(new SiteMenuItem { Id = 2, ParentId = 0, Text = "Products", Url = url.Action("Index", "Product"), OpenInNewWindow = false, SortOrder = 2 });
            items.Add(new SiteMenuItem { Id = 3, ParentId = 0, Text = "Complaint Type", Url = url.Action("Index", "ComplaintType"), OpenInNewWindow = false, SortOrder = 3 });
            items.Add(new SiteMenuItem { Id = 4, ParentId = 0, Text = "Data Design", Url = url.Action("Index", "CustomForm"), OpenInNewWindow = false, SortOrder = 4 });

            // 4
            items.Add(new SiteMenuItem { Id = 5, ParentId = 4, Text = "Configure", Url = url.Action("Index", "FormConfig"), OpenInNewWindow = false, SortOrder = 1 });
            items.Add(new SiteMenuItem { Id = 6, ParentId = 4, Text = "More Information - Form", Url = url.Action("Index", "CustomForm"), OpenInNewWindow = false, SortOrder = 2 });
            items.Add(new SiteMenuItem { Id = 7, ParentId = 4, Text = "Data", Url = url.Action("Index", "DataSource"), OpenInNewWindow = false, SortOrder = 3 });
            items.Add(new SiteMenuItem { Id = 8, ParentId = 4, Text = "Data List", Url = url.Action("Index", "DataValue"), OpenInNewWindow = false, SortOrder = 4 });


            SelectItem(context, items);

            return items;
        }

        private static void SelectItem(RequestContext context, List<ISiteLink> items)
        {
            var path = context.HttpContext.Request.FilePath;

            items.ForEach(i =>
            {
                if (i.Url == path)
                    if (i.ParentId != 0)
                    {
                        items.Find(p => p.Id == i.ParentId).Selected = true;
                    }
                    else
                    {
                        i.Selected = true;
                    }
            });
        }
    }
}
